#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        for (auto i : nums)
        {
            sum += i;
        }
        int cha = sum - x;
        int left, right;
        int bigsum = 0;
        int len = -1;
        int n = nums.size();
        for (left = right = 0; right < n; right++)
        {
            bigsum += nums[right];
            while (left <= right && bigsum > cha)
            {
                bigsum -= nums[left++];
            }
            if (bigsum == cha)
            {
                if (len < right - left + 1)
                    len = right - left + 1;
            }
        }
        return len == -1 ? -1 : n - len;
    }
};